package cn.wolfcode.wolf2w.business.listener;

import cn.wolfcode.wolf2w.business.api.domain.Note;
import cn.wolfcode.wolf2w.business.service.INoteService;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class ApplicationInitListener implements ApplicationListener<ApplicationReadyEvent> {


    @Autowired
    private INoteService noteService;
    @Autowired
    private RedisService redisService;
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.err.println("应用程序准备完成");
        for (Note note : noteService.list()) {
            String key = RedisKeys.NOTE_STATIS_HASH.join(note.getId().toString());
            if (redisService.hasKey(key)){
                continue;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("viewnum", Integer.valueOf(note.getViewnum().toString()));
            map.put("replynum", Integer.valueOf(note.getReplynum().toString()));
            map.put("sharenum", Integer.valueOf(note.getSharenum().toString()));
            map.put("favornum", Integer.valueOf(note.getFavornum().toString()));
            map.put("thumbsupnum", Integer.valueOf(note.getThumbsupnum().toString()));
            map.put("id",Integer.valueOf(note.getId().toString()));
            redisService.setCacheMap(key,map);




        }

    }
}